package org.jw.testemunhopublico.util;

import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	public static Date primeiroDiaDoMes(Date given) {
	    Calendar givenCal = Calendar.getInstance();
	    givenCal.setTime(given);
	    
	    int month = givenCal.get(Calendar.MONTH);
	    int year = givenCal.get(Calendar.YEAR);

	    Calendar cal = Calendar.getInstance();
	    cal.clear();
	    cal.set(Calendar.YEAR, year);
	    cal.set(Calendar.MONTH, month);
	    cal.set(Calendar.DAY_OF_MONTH, 1);
	    Date dueDate = new Date(cal.getTimeInMillis());

	    return dueDate;
	}
	
	public static Date ultimoDiaDoMes(Date given) {
	    Calendar givenCal = Calendar.getInstance();
	    givenCal.setTime(given);
	    
	    int month = givenCal.get(Calendar.MONTH) + 1;
	    int year = givenCal.get(Calendar.YEAR);

	    Calendar cal = Calendar.getInstance();
	    cal.clear();
	    cal.set(Calendar.YEAR, year);
	    cal.set(Calendar.MONTH, month);
	    cal.set(Calendar.DAY_OF_MONTH, 1);
	    Date dueDate = new Date(cal.getTimeInMillis() - 86400000);

	    return dueDate;
	}

}
